package com.k.liuzh.extraffic.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.k.liuzh.extraffic.Database.EnvDao;
import com.k.liuzh.extraffic.Database.EnvironmentBean;
import com.k.liuzh.extraffic.R;

import org.json.JSONException;
import org.json.JSONObject;

public class HuanJing_Fragment extends Fragment {
    @Nullable
    private View view;
    /**
     * 32
     */
    private TextView mTextviewHuanjingWendu;
    private LinearLayout mLayoutHuanjingWendu;
    /**
     * 32
     */
    private TextView mTextviewHuanjingShidu;
    private LinearLayout mLayoutHuanjingShidu;
    /**
     * 32
     */
    private TextView mTextviewHuanjingGuangzhao;
    private LinearLayout mLayoutHuanjingGuangzhao;
    /**
     * 32
     */
    private TextView mTextviewHuanjingCq2;
    private LinearLayout mLayoutHuanjingCq2;
    /**
     * 32
     */
    private TextView mTextviewHuanjingPm25;
    private LinearLayout mLayoutHuanjingPm25;
    /**
     * 32
     */
    private TextView mTextviewHuanjingRoad;
    private LinearLayout mLayoutHuanjingRoad;


    private RequestQueue requestQueue;
    private static final String TAG = "HuanJing_Fragment";
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_huanjing, container, false);
        requestQueue =  Volley.newRequestQueue(getContext());
        initView(view);
        getData();
        return view;
    }

    private void getData() {
        final int[] temperature = {0};
        final int[] humidity = {0};
        final int[] LightIntensity = {0};
        final int[] cq2 = {0};
        final int[] pm25 = {0};
        final int[] road = {0};

        Bundle bundle = getArguments();
        String userName = bundle.getString("userName");
        String queryUrl = "http://47.106.153.72:8081/transportservice/action/GetAllSense.do";
        String queryRoad = "http://47.106.153.72:8081/transportservice/action/GetRoadStatus.do";
        final JSONObject js = new JSONObject();
        try {
            js.put("UserName",userName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JSONObject js1 = new JSONObject();
        try {
            js1.put("RoadId",1);
            js1.put("UserName",userName);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.POST, queryRoad, js1, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d(TAG, "onResponse: 22222222222222222222222222222");
                try {
                    road[0] = jsonObject.getInt("Status");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int a,b,c,d,e,f;
                a = temperature[0];
                b = humidity[0];
                c = LightIntensity[0];
                d = cq2[0];
                e = pm25[0];
                f = road[0];
                Log.d(TAG, "onResponse: "+a+b+c+d+e+f);
                EnvironmentBean bean = new EnvironmentBean(a,b,c,d,e,f);
                new EnvDao(getContext()).add(bean);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, queryUrl, js, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d(TAG, "onResponse: 111111111111111111111111111");

                try {
                     temperature[0] = jsonObject.getInt("temperature");
                    humidity[0] = jsonObject.getInt("humidity");
                    LightIntensity[0] = jsonObject.getInt("LightIntensity");
                    cq2[0] = jsonObject.getInt("co2");
                    pm25[0] = jsonObject.getInt("pm2.5");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Log.d(TAG, "onResponse: "+temperature[0]);

                requestQueue.add(jsonObjectRequest1);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });


        requestQueue.add(jsonObjectRequest);


    }

    private void initView(View view) {
        mTextviewHuanjingWendu = (TextView) view.findViewById(R.id.textview_huanjing_wendu);
        mLayoutHuanjingWendu = (LinearLayout) view.findViewById(R.id.layout_huanjing_wendu);
        mTextviewHuanjingShidu = (TextView) view.findViewById(R.id.textview_huanjing_shidu);
        mLayoutHuanjingShidu = (LinearLayout) view.findViewById(R.id.layout_huanjing_shidu);
        mTextviewHuanjingGuangzhao = (TextView) view.findViewById(R.id.textview_huanjing_guangzhao);
        mLayoutHuanjingGuangzhao = (LinearLayout) view.findViewById(R.id.layout_huanjing_guangzhao);
        mTextviewHuanjingCq2 = (TextView) view.findViewById(R.id.textview_huanjing_cq2);
        mLayoutHuanjingCq2 = (LinearLayout) view.findViewById(R.id.layout_huanjing_cq2);
        mTextviewHuanjingPm25 = (TextView) view.findViewById(R.id.textview_huanjing_pm25);
        mLayoutHuanjingPm25 = (LinearLayout) view.findViewById(R.id.layout_huanjing_pm25);
        mTextviewHuanjingRoad = (TextView) view.findViewById(R.id.textview_huanjing_road);
        mLayoutHuanjingRoad = (LinearLayout) view.findViewById(R.id.layout_huanjing_road);
    }
}
